package com.nianzhen.recruitment;

import com.lidroid.xutils.util.LogUtils;
import com.nianzhen.utils.ActivityCollector;

import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

public abstract class BaseFragmentActivity extends FragmentActivity {

    private ImageView mBackBtn;
    private TextView mTitleTv;
    private TextView mRightTv;

    /*
	 * 自动管理活动的创建销毁
	 * 
	 */
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		LogUtils.d(getClass().getSimpleName());
		ActivityCollector.addActivity(this);
	}

	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
		ActivityCollector.removeActivity(this);
	}
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// TODO Auto-generated method stub
		menu.add(1, Menu.FIRST + 1, 1, "退出").setIcon(android.R.drawable.ic_menu_close_clear_cancel);
		//menu.add(1, Menu.FIRST + 2, 2, "主页").setIcon(android.R.drawable.ic_menu_search);
		return true;
	}
	
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// TODO Auto-generated method stub
		switch (item.getItemId()) {
		case Menu.FIRST+1:
			ActivityCollector.finishAll();
			break;
		case Menu.FIRST+2:
//			Intent intent= new Intent(BaseActivity.this,HomePager.class);
//			startActivity(intent);
		default:
			break;
		}
		return true;
	}
    
    @Override
    protected void onResume() {
        super.onResume();
       
    }

    @Override
    protected void onPause() {
        super.onPause();
       
    }

    @Override
    public void setContentView(int layoutResID) {
        super.setContentView(layoutResID);
        initTitle();
    }

    protected void initTitle() {
        mBackBtn = (ImageView) findViewById(R.id.header_back_btn);
        mRightTv = (TextView) findViewById(R.id.header_right_tv);
        mTitleTv = (TextView) findViewById(R.id.header_tv);
        if (mBackBtn != null) {
            mBackBtn.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View arg0) {
                    if (onInterceptBack()) {
                        return;
                    } else {
                        onLeftPressed();
                    }
                }
            });
        }

        if (mRightTv != null) {
            mRightTv.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    onRightPressed();
                }
            });
        }
    }

    public boolean onInterceptBack() {
        return false;
    }

    public void onRightPressed() {

    }

    public void onLeftPressed() {
        finish();
    }

    @Override
    public void setTitle(int resId) {
        if (mTitleTv != null) {
            mTitleTv.setText(getString(resId));
        }
    }

    public void setTitle(String str) {
        if (mTitleTv != null) {
            mTitleTv.setText(str);
        }
    }

    public void setHeaderRightText(String str){
        if (mRightTv!=null){
            mRightTv.setText(str);
        }
    }

    public void showBackBtn() {
        if (mBackBtn != null) {
            mBackBtn.setVisibility(View.VISIBLE);
        }
    }

    public void hideBackBtn() {
        if (mBackBtn != null) {
            mBackBtn.setVisibility(View.GONE);
        }
    }

	public void setHeaderRightText(int resId){
		if(mRightTv != null){
            mRightTv.setText(resId);
		}
	}

}
